package com.qfedu.edu.result;

import com.qfedu.edu.enum1.ResponseCode;
import lombok.Data;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


@Data
public class R {
    private Integer code;
    private String message;
    private Map<String, Object> dataMap=new HashMap<>();

    private R(){}

    public static R ok(){
        R r = new R();
        r.setCode(ResponseCode.SUCCESS.getCode());
        r.setMessage(ResponseCode.SUCCESS.getMessage());
        return r;
    }

    public static R error(){
        R r = new R();
        r.setCode(ResponseCode.ERROR.getCode());
        r.setMessage(ResponseCode.ERROR.getMessage());
        return r;
    }

    public static R error(Integer code,String message){
        R r = new R();
        r.setCode(code);
        r.setMessage(message);
        return r;
    }

    public static R error(ResponseCode responseCode){
        R r = new R();
        r.setCode(responseCode.getCode());
        r.setMessage(responseCode.getMessage());
        return r;
    }

    public R data(String key,Object value){
        this.getDataMap().put(key,value);
        return this;
    }

    public R data(Map<String,Object> data){
        Set<Map.Entry<String, Object>> entries = data.entrySet();
        Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<String, Object> next = iterator.next();
            String key = next.getKey();
            Object value = next.getValue();
            this.getDataMap().put(key, value);
        }
        return this;
    }
}
